import { readFile } from 'fs/promises';
import YAML from 'yaml';

export async function loadYaml<T = unknown>(file: string): Promise<T> {
  let text;
  try {
    text = await readFile(file, 'utf-8');
  } catch (err) {
    console.error(err);
    throw new Error('not found');
  }
  if (!text.startsWith('---')) {
    throw new Error('not found');
  }
  // console.log(text);
  return YAML.parse(text);
}
